package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.pojo.NjtUserRegister;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.NjtUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class NjtUserRegisterServiceImpl implements NjtUserRegisterService {
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;

    @Override
    public List<NjtUserRegister> getNjtUserRegisterList(NjtUserRegister njtUserRegister) {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    @Override
    public int updateUserRegister(NjtUserRegister njtUserRegister) {
        njtUserRegister.setUpdateBy(SecurityUtils.getUsername());
        njtUserRegister.setUpdateTime(new Date());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    @Override
    public int deleteUserRegister(Long id) {
        return njtUserRegisterMapper.alterNjtUserRegister(id);
    }

    @Override
    public int alertUserRegister(Long id) {
        return njtUserRegisterMapper.alterNjtUserRegister(id);
    }

    @Override
    public NjtUserRegister userLogin(NjtUserRegister njtUserRegister) {
        return njtUserRegisterMapper.userLogin(njtUserRegister);
    }

    @Override
    public NjtUserRegister getUserByPhone(String phone) {
        return njtUserRegisterMapper.getUserByPhone(phone);
    }

    @Override
    public int insertNjtUserRegister(NjtUserRegister userRegister) {
        // 设置创建时间（如果前端没传）
        if (userRegister.getRegisterTime() == null) {
            userRegister.setRegisterTime(new Date());
        }
        userRegister.setCreateTime(new Date());
        userRegister.setUpdateTime(new Date());


        return njtUserRegisterMapper.insertNjtUserRegister(userRegister);
    }
    @Override
    public int resetPasswordByPhone(String mobilePhone, String newPassword) {
        return njtUserRegisterMapper.updatePasswordByPhone(mobilePhone, newPassword);
    }
}
